package khoi;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

class P extends JPanel {
	private int percent;

	public P(int percent) {
		this.percent = percent;
	}

	@Override
	public Dimension getPreferredSize() {
		Dimension d = getParent().getSize();
		int w = d.width * percent / 100;
		int h = d.height * percent / 100;
		return new Dimension(w, h);
	}
}

public class Example extends JFrame {

	public static void main(String args[]) {
		new Example();
	}

	public Example() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel p = new JPanel();
		setContentPane(p);
		p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));

		JPanel panel1 = new P(50);
		panel1.setBackground(Color.RED);
		JPanel panel2 = new P(50);
		panel2.setBackground(Color.GREEN);
		p.add(panel1);
		p.add(panel2);
		setSize(500, 500);
		setVisible(true);
	}
}